<?php

/**
 * Observable part of the observer pattern. An observable is an object that
 * notifies observers about changes.
 *
 * @package be.appcraft.util
 * @version $Revision: 1.1 $
 *
 * @author Stijn Van Bael
 * @since 1.0
 */
abstract class Pageley_Util_Observable {
	
	/**
	 * A list of observers observing this object.
	 *
	 * @var Map
	 */
	private $observers;
	
	/**
	 * Creates a new Observable
	 *
	 */
	public function __construct() {
		$this->observers = new Pageley_Util_ArrayMap();
	}
	
	/**
	 * Adds an observer to this observable. Once an observer has been added,
	 * it will receive notificaties about changes of Observable.
	 *
	 * @param Observer $observer: the observer to add
	 */
	public function addObserver($observer) {
		$this->observers->add($observer);
	}
	
	/**
	 * Removes an observer from the observers observing this Observable.
	 *
	 * @param Observer $observer: the observer to remove
	 */
	public function removeObserver($observer) {
		$this->observers->removeValue($observer);
	}
	
	/**
	 * Notifies all observers observing this Observable about a change.
	 *
	 * @param object $data: additional data related to the update
	 */
	public function notifyObservers($data = null) {
		foreach($this->observers as $observer) {
			$observer->notify($this, $data);
		}
	}
}